﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;

namespace NearForums.DataAccess
{
    public class TopicTypeDataAccess : BaseDataAccess,ITopicTypeDataAccess
    {
        public void Add(TopicType topicType)
        {
            DbCommand comm = this.GetCommand("SPTopicTypeInsert");
            
            comm.AddParameter(this.Factory, "TypeName", DbType.String, topicType.TypeName);
            comm.AddParameter(this.Factory, "ForumID", DbType.Int32, topicType.Forum.Id);

            comm.SafeExecuteNonQuery();
        }

        public void UpdateTopicType(TopicType topicType)
        {
            DbCommand comm = this.GetCommand("SPTopicTypeUpdate");
            comm.AddParameter(this.Factory, "TypeID", DbType.Int32, topicType.TypeID);
            comm.AddParameter(this.Factory, "TypeName", DbType.String, topicType.TypeName);

            comm.SafeExecuteNonQuery();
        }

        public List<TopicType> GetTopicTypesByForum(int forumId)
        {
            var comm = GetCommand("SPTopicTypeGetByForumID");
            comm.AddParameter(Factory, "ForumID", DbType.Int16, forumId);
            var dt = GetTable(comm);

            var topicTypeList = new List<TopicType>();

            foreach (DataRow dr in dt.Rows)
            {

                TopicType type = ParseTopicTypeDataRow(dr);
                Forum forum = new Forum();
                forum.Id = forumId;
                type.Forum = forum;

                topicTypeList.Add(type);   
            }

            return topicTypeList;
        }

        protected virtual TopicType ParseTopicTypeDataRow(DataRow dr)
        {
            TopicType f = new TopicType();
            f.TypeID = dr.Get<int>("TypeID");
            f.TypeName = dr.GetString("TypeName");
            f.TypeShortName = dr.GetString("TypeShortName");

            return f;
        }

        public void AddTopicTypeRelation(TopicTypeRelation topicTypeRel)
        {
            DbCommand comm = this.GetCommand("SPTopicTypeRelInsert");
            comm.AddParameter(this.Factory, "TopicID", DbType.Int16, topicTypeRel.TopicID);
            comm.AddParameter(this.Factory, "TopicTypeID", DbType.Int16, topicTypeRel.Type.TypeID);
            
            comm.SafeExecuteNonQuery();
        }

        public void UpdateTopicTypeRelation(TopicTypeRelation topicTypeRel)
        {
            DbCommand comm = this.GetCommand("SPTopicTypeRelUpdate");
            comm.AddParameter(this.Factory, "TopicID", DbType.Int16, topicTypeRel.TopicID);
            comm.AddParameter(this.Factory, "TopicTypeID", DbType.Int16, topicTypeRel.Type.TypeID);

            comm.SafeExecuteNonQuery();
        }

        public void RemoveTopicTypeRelation(TopicTypeRelation topicTypeRel)
        {
            DbCommand comm = this.GetCommand("SPTopicTypeRelDelete");
            comm.AddParameter(this.Factory, "TopicID", DbType.Int16, topicTypeRel.TopicID);
            comm.AddParameter(this.Factory, "TopicTypeID", DbType.Int16, topicTypeRel.Type.TypeID);

            comm.SafeExecuteNonQuery();
        }
    }
}
